﻿Imports System.Windows.Forms

Public Class ConfigUI
    Private Const POP3Plugin As String = "MailNotify.POP3Plugin"
    Private Const POP3SSLPlugin As String = "MailNotify.SSLPlugin"
    Private Const IMAPPlugin As String = "MailNotify.IMapPlugin"
    Private Const IMAPSSLPlugin As String = "MailNotify.IMapSSLPlugin"
    Private Const POP3Port As String = "110"
    Private Const IMAPPort As String = "143"
    Private Const POP3SSLPort As String = "995"
    Private Const IMAPSSLPort As String = "993"

    Private _CurrentPlugin As String = POP3Plugin
    Private _CurrentAccount As String
    Private Sub LoadSettings()
        Dim config As New Configbase(Of AccountSettings)(Me._CurrentAccount)
        Dim accntSettings As AccountSettings = config.ReadSettings
        With accntSettings
            Me.uxAccountName.Text = .AccountName
            Me.uxPassword.Text = .Password
            Me.uxServerIp.Text = .ServerIp
            Me.uxPortNum.Text = .ServerPort
            Me.uxUsername.Text = .Username
            Me._CurrentPlugin = .Plugin
            Me.uxSSL.Checked = .IsSSLEnabled
            If [Enum].Parse(GetType(MailServerType), .ServerType) = MailServerType.POP3 Then
                Me.uxPOP3.Checked = True
                Me.uxImap.Checked = False
            Else
                Me.uxPOP3.Checked = False
                Me.uxImap.Checked = True
            End If
        End With
        config = Nothing
        accntSettings = Nothing
    End Sub
    Sub New()
        InitializeComponent()
    End Sub
    Sub New(ByVal AccountName As String)
        Me.InitializeComponent()
        Me._CurrentAccount = AccountName
        Me.LoadSettings()
        Me.uxAccountName.Enabled = False
    End Sub
    Private Sub uxSSL_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSSL.CheckedChanged
        UpdatePort()
    End Sub
    Private Sub ServerType_CheckChanged(ByVal sender As Object, ByVal e As EventArgs) Handles uxPOP3.CheckedChanged, uxImap.CheckedChanged
        Me.UpdatePort()
    End Sub
    Private Sub UpdatePort()
        If uxSSL.Checked Then
            If uxPOP3.Checked Then
                Me.uxPortNum.Text = POP3SSLPort
                _CurrentPlugin = POP3SSLPlugin
            Else
                Me.uxPortNum.Text = IMAPSSLPort
                _CurrentPlugin = IMAPSSLPlugin
            End If
        Else
            If uxPOP3.Checked Then
                Me.uxPortNum.Text = POP3Port
                _CurrentPlugin = POP3Plugin
            Else
                Me.uxPortNum.Text = IMAPPort
                _CurrentPlugin = IMAPPlugin
            End If
        End If
    End Sub
    Private Sub uxSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxSave.Click
        If Me.uxAccountName.TextLength <= 0 Then
            Utility.ShowMessage("Account name cannot be empty")
            Me.uxAccountName.Focus()
            Return
        End If

        If Me.uxServerIp.TextLength <= 0 Then
            Utility.ShowMessage("Server name or IP address cannot be empty")
            Me.uxServerIp.Focus()
            Return
        End If

        If Me.uxPortNum.TextLength <= 0 Then
            Utility.ShowMessage("Port cannot be empty")
            Me.uxPortNum.Focus()
            Return
        End If

        If Not IsNumeric(Me.uxPortNum.Text) Then
            Utility.ShowMessage("Port should be numeric")
            Me.uxPortNum.Focus()
            Return
        End If

        If Me.uxUsername.TextLength <= 0 Then
            Utility.ShowMessage("Username cannot be empty")
            Me.uxUsername.Focus()
            Return
        End If

        Dim accntSettings As New AccountSettings
        With accntSettings
            .AccountName = Me.uxAccountName.Text
            .Password = Me.uxPassword.Text
            .ServerIp = Me.uxServerIp.Text
            .ServerPort = Me.uxPortNum.Text
            .Username = Me.uxUsername.Text
            .IsSSLEnabled = Me.uxSSL.Checked
            .Plugin = Me._CurrentPlugin

            If Me.uxPOP3.Checked Then
                .ServerType = MailServerType.POP3
            Else
                .ServerType = MailServerType.IMAP
            End If

        End With
        Dim config As New Configbase(Of AccountSettings)(Me.uxAccountName.Text)
        config.SaveSettings(accntSettings)
        Me.Close()
    End Sub

    Private Sub uxExportAccount_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles uxExportAccount.Click
        Dim filename As String = String.Empty
        Using saveDialog As New SaveFileDialog
            With saveDialog
                .AddExtension = True
                .AutoUpgradeEnabled = False
                .CheckPathExists = True
                .FileName = String.Concat(Me.uxAccountName.Text, ".config")
                .DefaultExt = "config"
                .Filter = "Config Files(*.config)|*.config|All Files(*.*)|*.*"
                .OverwritePrompt = True
                .SupportMultiDottedExtensions = True
                .ValidateNames = True
                .Title = "Export Config"
                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    filename = .FileName
                    Dim accntSettings As New AccountSettings
                    With accntSettings
                        .AccountName = Me.uxAccountName.Text
                        .Password = Me.uxPassword.Text
                        .ServerIp = Me.uxServerIp.Text
                        .ServerPort = Me.uxPortNum.Text
                        .Username = Me.uxUsername.Text
                        .Plugin = Me._CurrentPlugin
                        .IsSSLEnabled = Me.uxSSL.Checked
                        If Me.uxPOP3.Checked Then
                            .ServerType = MailServerType.POP3
                        Else
                            .ServerType = MailServerType.IMAP
                        End If
                        .SaveConfig(filename)
                    End With
                End If
            End With
        End Using
    End Sub
End Class